//
//  ClassifyViewController.m
//  BiDongComic
//
//  Created by 吴炳毅 on 16/3/14.
//  Copyright © 2016年 BestRiven. All rights reserved.
//


#define SUGGESURLSTR @"http://api.kuaikanmanhua.com/v1/tag/suggestion"

#define STR @"http://api.kuaikanmanhua.com/v1/topics?limit=20&offset=0&tag=%E7%9F%AD%E7%AF%87"

#import "ClassifyViewController.h"
#import "AFNetworking/AFNetworking.h"
#import "ClassifyModel.h"
#import "ClassifyCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "ListViewController.h"
#import "ComicGridViewController.h"

//%E7%9F%AD%E7%AF%87

@interface ClassifyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    NSMutableArray *arrayAllData;
}

@end

@implementation ClassifyViewController




- (void)loadView {
    self.navigationItem.hidesBackButton = YES;

    self.classifyView = [[ClassifyView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.classifyView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDatas];
    [self setDelegate];

    // Do any additional setup after loading the view.
}

#pragma mark - 数据设置
- (void)setDatas {
    //请求分组网络数据
    arrayAllData = [NSMutableArray array];
    
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:SUGGESURLSTR parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        for (NSDictionary *dic in responseObject[@"data"][@"suggestion"]) {
            ClassifyModel *model = [[ClassifyModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [arrayAllData addObject:model];
        
            
        }
    
        //回到主线程 给ui赋值
        [self performSelectorOnMainThread:@selector(mainAction) withObject:nil waitUntilDone:YES];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"失败");
    }];

}



#pragma mark - 请求数据回到主线程
- (void)mainAction {
    [self.classifyView.collectionView_classify reloadData];
    
}


#pragma mark - 设置集合视图代理
- (void)setDelegate {
    self.classifyView.collectionView_classify.delegate = self;
    self.classifyView.collectionView_classify.dataSource = self;
    //
    self.classifyView.collectionView_classify.backgroundColor = [UIColor colorWithRed:256 green:0 blue:0 alpha:0.3];
    
    
    // 4.注册cell的类型和重用标示符
    [self.classifyView.collectionView_classify registerClass:[ClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"cell_id_collectionCell"];


}

#pragma mark - UICollectionViewDataSource Methods
#pragma mark 设置有多少个section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 5;
}

#pragma mark 设置某个分组有多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

#pragma mark 设置某个Item显示什么内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell_id_collectionCell" forIndexPath:indexPath];
    cell.imV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    cell.imV.layer.masksToBounds = YES;
    cell.imV.layer.cornerRadius = 40;
    cell.lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 80, 20)];
    if (arrayAllData.count != 0) {
    ClassifyModel *model = arrayAllData[indexPath.section*3+indexPath.row];
    [cell.imV sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    [cell.contentView addSubview:cell.imV];
     cell.imV.backgroundColor = [UIColor cyanColor];
        cell.lable.text = model.title;
        cell.lable.font = [UIFont systemFontOfSize:12];
        cell.lable.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:cell.lable];
        

    }

    
    return cell;
}

#pragma mark 处理点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyModel *model = arrayAllData[indexPath.section*3+indexPath.row];
    

    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:model.title];
    NSString *urlStr = [model.title stringByAddingPercentEncodingWithAllowedCharacters:set ];
    NSString *stringURL = [NSString stringWithFormat:@"http://api.kuaikanmanhua.com/v1/topics?limit=20&offset=0&tag=%@",urlStr];


    NSLog(@"%@",stringURL);
    
    ListViewController *listVC = [[ListViewController alloc]init];
    listVC.sessionUrlStr = stringURL;
    [self.navigationController pushViewController:listVC animated:YES];
    listVC.title = model.title;
    listVC.more_select = 2;

    
    
    
    
}

#pragma mark - UICollectionViewDelegateFlowLayout Method
#pragma mark 设置item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 100);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
